home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / BBS-Archive / Comm / AmiTCP30b2.lha / src / rpclib / get_myaddress.c < prev    next >
C/C++ Source or Header  |  1994-03-09  |  4KB  |  143 lines

  1. /*
  2.  * $Id: get_myaddress.c,v 1.3 1994/02/26 18:52:47 jraja Exp $
  3.  *
  4.  * $Log: get_myaddress.c,v $
  5.  * Revision 1.3  1994/02/26  18:52:47  jraja
  6.  * Added code using gethostid() if AMITCP is defined.
  7.  *
  8.  * Revision 1.2  1993/11/10  01:55:31  jraja
  9.  * Fixed includes.
  10.  * Search for non-loopback address if possible.
  11.  * Changed ioctl() and close() to IoctlSocket() and CloseSocket() for AMITCP.
  12.  *
  13.  */
  14. /* @(#)get_myaddress.c    2.1 88/07/29 4.0 RPCSRC */
  15. /*
  16.  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  17.  * unrestricted use provided that this legend is included on all tape
  18.  * media and as a part of the software program in whole or part.  Users
  19.  * may copy or modify Sun RPC without charge, but are not authorized
  20.  * to license or distribute it to anyone else except as part of a product or
  21.  * program developed by the user.
  22.  * 
  23.  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  24.  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  25.  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  26.  * 
  27.  * Sun RPC is provided with no support and without any obligation on the
  28.  * part of Sun Microsystems, Inc. to assist in its use, correction,
  29.  * modification or enhancement.
  30.  * 
  31.  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  32.  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  33.  * OR ANY PART THEREOF.
  34.  * 
  35.  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  36.  * or profits or other special, indirect and consequential damages, even if
  37.  * Sun has been advised of the possibility of such damages.
  38.  * 
  39.  * Sun Microsystems, Inc.
  40.  * 2550 Garcia Avenue
  41.  * Mountain View, California  94043
  42.  */
  43. #if !defined(lint) && defined(SCCSIDS)
  44. static char sccsid[] = "@(#)get_myaddress.c 1.4 87/08/11 Copyr 1984 Sun Micro";
  45. #endif
  46.  
  47. /*
  48.  * get_myaddress.c
  49.  *
  50.  * Get client's IP address via ioctl.  This avoids using the yellowpages.
  51.  * Copyright (C) 1984, Sun Microsystems, Inc.
  52.  */
  53.  
  54. #include <sys/param.h>
  55. #include <rpc/types.h>
  56. #include <rpc/xdr.h>
  57. #include <rpc/auth.h>
  58. #include <rpc/clnt.h>
  59. #include <rpc/pmap_prot.h>
  60. #include <sys/socket.h>
  61. #include <stdio.h>
  62. #include <net/if.h>
  63. #include <sys/ioctl.h>
  64. #include <netinet/in.h>
  65. #include <arpa/inet.h>
  66.  
  67. /* 
  68.  * don't use gethostbyname, which would invoke yellow pages
  69.  */
  70. void
  71. get_myaddress(struct sockaddr_in *addr)
  72. {
  73. #ifdef AMITCP
  74.   /*
  75.    * AmiTCP gethostid() is documented to return our primary IP address
  76.    * as the host id, use it.
  77.    */
  78.   static struct sockaddr_in saddr = { sizeof(struct sockaddr_in),
  79.                     AF_INET,
  80.                     htons(PMAPPORT),
  81.                     0 };
  82.   *addr = saddr;
  83.   addr->sin_addr.s_addr = gethostid();
  84. #else
  85.     int s;
  86.     char buf[BUFSIZ];
  87.     struct ifconf ifc;
  88.     struct ifreq ifreq, *ifr;
  89.     int len, slop;
  90.  
  91.     if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
  92.         perror("get_myaddress: socket");
  93.         exit(1);
  94.     }
  95.     ifc.ifc_len = sizeof (buf);
  96.     ifc.ifc_buf = buf;
  97. #ifdef AMITCP
  98. #define ioctl IoctlSocket
  99. #endif
  100.     if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
  101.         perror("get_myaddress: ioctl (get interface configuration)");
  102.         exit(1);
  103.     }
  104.     ifr = ifc.ifc_req;
  105.     for (len = ifc.ifc_len; len; len -= sizeof ifreq) {
  106.         ifreq = *ifr;
  107.  
  108.         if (ioctl(s, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
  109.             perror("get_myaddress: ioctl");
  110.             exit(1);
  111.         }
  112.         if ((ifreq.ifr_flags & IFF_UP) &&
  113.             ifr->ifr_addr.sa_family == AF_INET) {
  114.             *addr = *((struct sockaddr_in *)&ifr->ifr_addr);
  115.             addr->sin_port = htons(PMAPPORT);
  116.  
  117.             /*
  118.              * Break on first non-loopback address. 
  119.              * Loopback address will remain if no other suitable
  120.              * address is found.
  121.              */
  122.                 if (!(ifreq.ifr_flags & IFF_LOOPBACK))
  123.                     break;
  124.         }
  125.         /*
  126.          * Deal with variable length addresses
  127.          */
  128.         slop = ifr->ifr_addr.sa_len - sizeof (struct sockaddr);
  129.         if (slop) {
  130.             ifr = (struct ifreq *) ((caddr_t)ifr + slop);
  131.             len -= slop;
  132.         }
  133.         ifr++;
  134.     }
  135. #ifdef AMITCP
  136. #undef ioctl
  137.     (void) CloseSocket(s);
  138. #else
  139.     (void) close(s);
  140. #endif
  141. #endif /* !AMITCP */
  142. }
  143.